package com.hisense.ovcloud.warehouse.routers;

import com.hisense.ovcloud.commons.configs.base.BaseResponse;
import com.hisense.ovcloud.commons.configs.validator.BaseRouterConfiguration;
import com.hisense.ovcloud.warehouse.dto.req.FetchLatestCertInnerReq;
import com.hisense.ovcloud.warehouse.dto.req.FetchLatestCertReq;
import com.hisense.ovcloud.warehouse.dto.resp.FetchLatestCertResp;
import com.hisense.ovcloud.warehouse.service.PemService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class CertRoutersConfiguration extends BaseRouterConfiguration {

    private final PemService pemService;

    public CertRoutersConfiguration(PemService pemService) {
        this.pemService = pemService;
    }

    @Bean
    public RouterFunction<ServerResponse> certsRoutes() {
        return RouterFunctions.nest(path("/"),
                route()
                        .POST("/fetchLastestGa", req -> ServerResponse.ok().body(pemService.fetchLatestCert(req.bodyToMono(FetchLatestCertReq.class)),
                                FetchLatestCertResp.class))
                        .POST("/ac-julink-warehouse/fetchLastestGaInner", req -> ServerResponse.ok().body(pemService.fetchLatestCertInner(req.bodyToMono(FetchLatestCertInnerReq.class)),
                                BaseResponse.class))
                        .build());
    }
}
